﻿/*
 * Copy Pasta
 * Created by Trevor Philip
 * 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * 
 * Class:       CfrmMainForm
 * Purpose:     Main form of the program
 * 
 * 
 */



using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.OleDb;

namespace MyForumWatcher
{
    public partial class CfrmMainForm : Form
    {

        //public ArrayList AllForumMembers { get; set; }

        #region Other Form Properties
        public BindingSource BSForums { get; set; }
        public BindingSource BSCategories { get; set; }
        public BindingSource BSThreadType { get; set; }
        public BindingSource BSPost { get; set; }
        #endregion

        #region constructor
        public CfrmMainForm()
        {
            InitializeComponent();

            //Assigning binding source objects and data sources.

            BSForums                            =   new BindingSource();
            BSForums.DataSource                 =   databaseDataSet;
            BSForums.DataMember                 =   "Forums";
            BSForums.Sort                       =   "ForumsName";

            BSCategories                        =   new BindingSource();
            BSCategories.DataSource             =   BSForums;
            BSCategories.DataMember             =   "ForumsCategories";
            BSCategories.Sort                   =   "CategoryName";

            BSThreadType                        =   new BindingSource();
            BSThreadType.DataSource             =   BSCategories;
            BSThreadType.DataMember             =   "CategoriesThreadType";
            BSThreadType.Sort                   =   "ThreadType";

            BSPost                              =   new BindingSource();
            BSPost.DataSource                   =   BSThreadType;
            BSPost.DataMember                   =   "ThreadTypePost";
            BSPost.Sort                         =   "PostName";

            cmbForum.DataSource                 =   BSForums;
            cmbForum.DisplayMember              =   "ForumsName";

            cmbCategory.DataSource              =   BSCategories;
            cmbCategory.DisplayMember           =   "CategoryName";

            cmbThreadType.DataSource            =   BSThreadType;
            cmbThreadType.DisplayMember         =   "ThreadType";

            dgvDataGrid.DataSource              =   BSPost;
            dgvDataGrid.Columns[0].Visible      =   false;
            dgvDataGrid.Columns[1].Visible      =   false;
            dgvDataGrid.Columns[2].HeaderText   =   "Post Title";
            dgvDataGrid.Columns[3].Visible      =   false;

            txtAns.DataBindings.Add("Text", BSPost, "Post");

        }
        #endregion

        #region Event Handlers

        private void CfrmMainForm_Load(object sender, EventArgs e)
        {
            FillAllData();
        }

        private void btnAddForm_Click(object sender, EventArgs e)
        {
            CfrmAddNewDbItem addNew = new CfrmAddNewDbItem(1, this);
            addNew.ShowDialog();
        }

        private void btnAddCate_Click(object sender, EventArgs e)
        {
            if (cmbForum.SelectedIndex >= 0)
            {
                CfrmAddNewDbItem addNew = new CfrmAddNewDbItem(2, this);
                addNew.ShowDialog();
            }
            else
            {
                MessageBox.Show("Please make sure you have data under \"Forums\".", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAddThreadType_Click(object sender, EventArgs e)
        {
            if (cmbCategory.SelectedIndex >= 0)
            {
                CfrmAddNewDbItem addNew = new CfrmAddNewDbItem(3, this);
                addNew.ShowDialog();
            }
            else
            {
                MessageBox.Show("Please make sure you have data under \"Category\".", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAddPost_Click(object sender, EventArgs e)
        {
            if (cmbThreadType.SelectedIndex >= 0)
            {
                CfrmAddNewDbItem addNew = new CfrmAddNewDbItem(4, this);
                addNew.ShowDialog();
            }
            else
            {
                MessageBox.Show("Please make sure you have data under \"Thread Type\".", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDeleteForum_Click(object sender, EventArgs e)
        {
            if (cmbForum.SelectedIndex >= 0)
            {
                if (MessageBox.Show("Are you sure you wish to remove this item?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    BSForums.RemoveCurrent();
                    BSForums.EndEdit();
                    tableAdapterManager.ForumsTableAdapter.Update(databaseDataSet.Forums);
                    forumsTableAdapter.Fill(databaseDataSet.Forums);
                }
            }
            else
            {
                MessageBox.Show("There is nothing to delete", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDeleteCate_Click(object sender, EventArgs e)
        {
            if (cmbCategory.SelectedIndex >= 0)
            {
                if (MessageBox.Show("Are you sure you wish to remove this item?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    BSCategories.RemoveCurrent();
                    BSCategories.EndEdit();
                    tableAdapterManager.CategoriesTableAdapter.Update(databaseDataSet.Categories);
                    categoriesTableAdapter.Fill(databaseDataSet.Categories);
                }
            }
            else
            {
                MessageBox.Show("There is nothing to delete", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnDeleteThreadType_Click(object sender, EventArgs e)
        {
            if (cmbThreadType.SelectedIndex >= 0)
            {
                if (MessageBox.Show("Are you sure you wish to remove this item?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    BSThreadType.RemoveCurrent();
                    BSThreadType.EndEdit();
                    tableAdapterManager.ThreadTypeTableAdapter.Update(databaseDataSet.ThreadType);
                    threadTypeTableAdapter.Fill(databaseDataSet.ThreadType);
                }
            }
            else
            {
                MessageBox.Show("There is nothing to delete", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDeletePost_Click(object sender, EventArgs e)
        {
            if (cmbForum.SelectedIndex >= 0)
            {
                if (MessageBox.Show("Are you sure you wish to remove this item?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    BSPost.RemoveCurrent();
                    BSPost.EndEdit();
                    tableAdapterManager.PostTableAdapter.Update(databaseDataSet.Post);
                    postTableAdapter.Fill(databaseDataSet.Post);
                }
            }
            else
            {
                MessageBox.Show("There is nothing to delete", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSavePost_Click(object sender, EventArgs e)
        {
            if (dgvDataGrid.Rows.Count > 0)
            {
                try
                {
                    BSPost.EndEdit();
                    tableAdapterManager.PostTableAdapter.Update(databaseDataSet.Post);
                    postTableAdapter.Fill(databaseDataSet.Post);
                    MessageBox.Show("Save successful!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception error)
                {
                    MessageBox.Show("The operation could not complete successfully due to the following error(s) : \n\n." + error.Message,
                        "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("There is nothing to save. Please ensure you have added a post.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            CfrmAbout about = new CfrmAbout();
            about.ShowDialog();
        }
        #endregion

        #region Internal Methods

        public void FillAllData()
        {
            //Fill the table adapters

            FillPostData();
            FillThreadTypeData();
            FillCategoriesData();
            FillForumsData();

        }

        public void FillPostData()          { this.postTableAdapter.Fill(this.databaseDataSet.Post); }
        public void FillThreadTypeData()    { this.threadTypeTableAdapter.Fill(this.databaseDataSet.ThreadType); }
        public void FillCategoriesData()    { this.categoriesTableAdapter.Fill(this.databaseDataSet.Categories); }
        public void FillForumsData()        { this.forumsTableAdapter.Fill(this.databaseDataSet.Forums); }

        #endregion

    }
}

